#include <iostream>
#include <string>
#include <vector>
using namespace std;


struct ListNode {
    int val;
    ListNode *next;
    // ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    // ListNode(int x, ListNode *next) : val(x), next(next) {}
};

// ListNode* removeElements(ListNode* head, int val) {
   
//     while(head!=NULL && head->val==val)
//     {
//         ListNode *temp=head;
//         head = head->next;
//         delete temp;
//     }
//     ListNode* cur = head;
//         while (cur!=NULL&&cur->next!=NULL) {
//             if (cur->next->val == val) {
//                 ListNode* tmp = cur->next;
//                 cur->next = cur->next->next;
//                 delete tmp;
//             } else {
//                 cur = cur->next;
//             }
//         }

//     return head;
// }



class MyLinkedList {
public: 
    ListNode *_head;
    int len;

    MyLinkedList() {
        _head = new ListNode(0);
    }

    void InitNode(vector<int> v)
    {
        int v_size = v.size();
        ListNode *head;
        if(v_size==0)
        {
            return;
        }
        else if(v_size==1)
            head = new ListNode(v[0]);
        else if (v_size==2)
        {
            head = new ListNode(v[0]);
            head->next = new ListNode(v[1]);
        }
        else if(v_size>2)
        {
            ListNode *p, *q;
            head = new ListNode(v[0]);
            p = new ListNode(v[1]);
            head->next = p;
            for (int i = 2; i < v_size;i++)
            {
                q = new ListNode(v[i]);
                p->next = q;
                p = q;
            }
        }
        _head->next= head;
    }
    void length_linked_list(void)
    {
        len = 0;
        ListNode *temp=_head;
        while(temp)
        {
            if(temp->next!=nullptr)
                len++;
            temp = temp->next;
        }
    }

    int get(int index) {
        ListNode *temp = _head->next;
        int i=0;
        while(temp!=NULL)
        {
            if(index==i)
            {
                return temp->val;
            }
            else
            {
                i+=1;
                temp = temp->next;
            }
        }
        return -1;
    }
    
    void addAtHead(int val) {
        ListNode *temp = new ListNode(val);
        temp->next = _head->next;
        _head->next = temp;
    }
    
    void addAtTail(int val) 
    {
        ListNode *temp;
        temp = _head;
        while(temp!=NULL)
        {
            if(temp->next==nullptr)
                {
                    ListNode *tail = new ListNode(val);
                    temp->next = tail;
                    break;
                }
            else
            temp = temp->next;
        }
    }
    
    void addAtIndex(int index, int val) {
        length_linked_list();
        if(index<len)
        {
            ListNode *temp=_head;
            int i=0;
            while (temp)
            {
                if(i==index)
                {
                    ListNode *t = new ListNode(val);
                    t->next = temp->next;
                    temp->next = t;
                    break;
                };
                i++;
                temp = temp->next;
            }
        }
        else if(index==len)
            addAtTail(val);
    
    }
    
    void deleteAtIndex(int index) {
        length_linked_list();
        int i = 0;
        ListNode *temp = _head;
        if(index<len)
        {
            while(temp&&temp->next!=NULL)
            {
                if(i==index)
                {
                    ListNode *del_node = temp->next;
                    temp->next = temp->next->next;
                    
                    delete del_node;
                    break;
                }
                else
                {
                    i++;
                    temp = temp->next;
                }
            }
        }
    }
    void reverseList() {
        ListNode *p, *cul,*temp;
        cul = nullptr;
        p =_head->next;
        while(p)
        {
        temp = p->next;
        p->next = cul;
        cul = p;
        p = temp;
        }
        _head->next = cul;
    }
    void swapPairs(void) 
    {
        ListNode *cul=_head;
        while(cul&&cul->next&&cul->next->next)
        {
            ListNode *p=cul->next->next->next;
            cul->next->next->next=cul->next;
            cul->next= cul->next->next ; 
            cul->next->next->next=p;

            cul = cul->next->next;
        }
    }
    ~MyLinkedList() 
    { // 析构函数，用于释放链表内存
        while (_head) 
        {
            ListNode* temp = _head;
            _head = _head->next;
            delete temp;
        }
    }
};
int main() 
{
    ListNode *p;
    vector<int> v={1,2,3,4,5};
    MyLinkedList Node;
    Node.InitNode(v);
    // Node.addAtHead(5);
    // Node.addAtTail(10);
    // Node.deleteAtIndex(4);
    // Node.addAtIndex(0, 10);
    // Node.addAtIndex(0, 20); 
    // Node.addAtIndex(1, 30);
    // Node.reverseList();
    Node.swapPairs();
    for (p = Node._head->next; p != NULL; p = p->next)
    {
        cout << p->val<<endl;
    }
    Node.length_linked_list();
    cout <<endl<< Node.get(0)<<endl<<Node.len;
}